package com.xbai.content.controller;

import com.github.pagehelper.PageInfo;
import com.xbai.content.pojo.ContentCategory;
import com.xbai.content.service.ContentCategoryService;
import com.xbai.entity.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc ContentCategoryController
 * @author jxbai
 * @date 2021/1/28
 */

@RestController
@RequestMapping("/contentCategory")
@CrossOrigin
@RequiredArgsConstructor
public class ContentCategoryController {

    private final ContentCategoryService contentCategoryService;

    /***
     * ContentCategory分页条件搜索实现
     * @param contentCategory {@link ContentCategory}
     * @param page 页码
     * @param size 页大小
     * @return {@link Result<PageInfo>}
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) ContentCategory contentCategory, @PathVariable  int page, @PathVariable  int size){
        //调用ContentCategoryService实现分页条件查询ContentCategory
        PageInfo<ContentCategory> pageInfo = contentCategoryService.findPage(contentCategory, page, size);
        return new Result<>(pageInfo);
    }

    /***
     * ContentCategory分页搜索实现
     * @param page 页码
     * @param size 页大小
     * @return {@link Result<PageInfo>}
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用ContentCategoryService实现分页查询ContentCategory
        PageInfo<ContentCategory> pageInfo = contentCategoryService.findPage(page, size);
        return new Result<>(pageInfo);
    }

    /***
     * 多条件搜索ContentCategory数据
     * @param contentCategory {@link ContentCategory}
     * @return {@link Result}
     */
    @PostMapping(value = "/search" )
    public Result<List<ContentCategory>> findList(@RequestBody(required = false) ContentCategory contentCategory){
        //调用ContentCategoryService实现条件查询ContentCategory
        List<ContentCategory> list = contentCategoryService.findList(contentCategory);
        return new Result<>(list);
    }

    /***
     * 根据ID删除ContentCategory数据
     * @param id 主键id
     * @return {@link Result}
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Long id){
        //调用ContentCategoryService实现根据主键删除
        contentCategoryService.delete(id);
        return new Result();
    }

    /***
     * 修改ContentCategory数据
     * @param contentCategory {@link ContentCategory}
     * @param id 主键id
     * @return {@link Result}
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody ContentCategory contentCategory, @PathVariable Long id){
        //设置主键值
        contentCategory.setId(id);
        //调用ContentCategoryService实现修改ContentCategory
        contentCategoryService.update(contentCategory);
        return new Result();
    }

    /***
     * 新增ContentCategory数据
     * @param contentCategory {@link ContentCategory}
     * @return {@link Result}
     */
    @PostMapping
    public Result add(@RequestBody ContentCategory contentCategory){
        //调用ContentCategoryService实现添加ContentCategory
        contentCategoryService.add(contentCategory);
        return new Result();
    }

    /***
     * 根据ID查询ContentCategory数据
     * @param id 主键id
     * @return {@link Result}
     */
    @GetMapping("/{id}")
    public Result<ContentCategory> findById(@PathVariable Long id){
        //调用ContentCategoryService实现根据主键查询ContentCategory
        ContentCategory contentCategory = contentCategoryService.findById(id);
        return new Result<>(contentCategory);
    }

    /***
     * 查询ContentCategory全部数据
     * @return {@link Result}
     */
    @GetMapping
    public Result<List<ContentCategory>> findAll(){
        //调用ContentCategoryService实现查询所有ContentCategory
        List<ContentCategory> list = contentCategoryService.findAll();
        return new Result<>(list) ;
    }
}
